<template>
  <div class="main">
    <div id="wx_reg"></div>
  </div>
</template>


<script>
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'
import { getCodeImg, getWechatQrCode, checkWechatLoginStatus } from '@/api/login' // 假设你的 API 文件中增加了接口
import { LOGIN_USERNAME, LOGIN_PASSWORD, LOGIN_REMEMBERME } from '@/store/mutation-types'
import storage from 'store'
import WwLogin from '@/utils/wwLogin-1.2.7'

export default {
  data() {
    return {
      wwLoginConfig: {
        id: 'wx_reg',
        appid: 'wwd7a63ec9e4e28872',
        agentid: '1000002',
        redirect_uri: 'http%3A%2F%2Fhrmanagersystem2025.asia',
        // redirect_uri: "http://www.hrmanagersystem2025.asia/",
        state: 'state',
        href: '',
        lang: 'zh'
      }
    }
  },
  mounted() {
    var wwLogin = new WwLogin(this.wwLoginConfig)
    const urlParams = new URLSearchParams(window.location.search)
    let redirectUrl = urlParams.get('redirect')
    redirectUrl = redirectUrl.startsWith('/') ? redirectUrl.slice(1) : redirectUrl
    if (redirectUrl) {

      const decodedRedirect = decodeURIComponent(redirectUrl)
      const redirectParams = new URLSearchParams(decodedRedirect)
      const code = redirectParams.get('code')
      if (code) {
        this.enterpriseLogin2({
          code: code,
          corpid: 'wwd7a63ec9e4e28872'
        })
          .then((res) => this.loginSuccess(res))
          .catch(err => this.requestFailed(err))
          .finally(() => {
            this.logining = false;
          });
      }
    }
  },
  created() {

  },
  methods: {
    ...mapActions(['enterpriseLogin2']),
    requestFailed(err) {
      this.isLoginError = true
      this.loginErrorInfo = err
    },
    loginSuccess(res) {
      this.$router.push({ path: '/' })
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`
        })
      }, 1000)
      this.handleCloseLoginError()
    },
    handleCloseLoginError() {
      this.isLoginError = false
      this.loginErrorInfo = ''
    }
  },
  beforeDestroy() {

  }
}
</script>

<style lang="less" scoped>
#wx_reg {
  margin-top: 20px;
  padding: 30px;
  background-color: white;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
